package org.dalmatian.app.generator.config;

import java.util.Map;
import javax.sql.DataSource;
import lombok.RequiredArgsConstructor;
import org.anyline.data.datasource.DataSourceHolder;
import org.dalmatian.common.core.utils.SolonUtils;
import org.dalmatian.common.mybatis.dataSource.PlusDynamicDataSource;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Import;
import org.noear.solon.core.event.AppLoadEndEvent;
import org.noear.solon.core.event.EventListener;

/**
 * 系统启动监听器
 *
 * @author: opensnail
 * @date : 2021-11-19 19:00
 */
@Component
@RequiredArgsConstructor
public class MyBatisDataSourceMonitor implements EventListener<AppLoadEndEvent> {


    @Override
    public void onEvent(AppLoadEndEvent appLoadEndEvent) throws Throwable {
        // anyline 初始化 多数据源数据库
        PlusDynamicDataSource DS = SolonUtils.getBean("system");
        Map<String, DataSource> dsMap = DS.getDs();
        for (String dataName : dsMap.keySet()) {
            DataSourceHolder.reg(dataName, dsMap.get(dataName));
            System.out.println("初始化：" + dataName);
        }
    }
}
